package us.icitap.cm.business.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.eclipse.persistence.annotations.Customizer;

import us.icitap.cm.utils.MyCustomizer;

@Entity
@Customizer(MyCustomizer.class)
@Table(name="CASE",schema="MPC")
//@SequenceGenerator(name="CASE_SEQ", schema="MPC", initialValue=1, allocationSize=1)
@TableGenerator(name="CASE_IDS",table="AUTO_IDS",valueColumnName="LAST_ID",initialValue=1, allocationSize=1,schema="MPC")
public class Case implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = -4070991537876395029L;
	
	
	@Id
//	@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="CASE_SEQ")
	@GeneratedValue(strategy=GenerationType.TABLE,generator="CASE_IDS")
	private Integer id;
	
	@Column(name="CASE_ID")
	private String caseId;

	
	
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name="RECORD_DATE")
	private Date recordDate;
	
	@Column(name="RECORD_USER",length=60)
	private String recordUser;
	
	
	
	@OneToMany(mappedBy="policeCase")
	private List<Report> reportList;
	
	@ManyToOne
	@JoinColumn(name="LAST_REPORT_ID")
	private Report lastReport;
	
	
//	private Date openDate;
//	private CaseStatus caseStatus;
//	private List<CaseStatusHistory> caseStatusHistoryList;
	
	
	
	

	public Case() {
		super();
		// TODO Auto-generated constructor stub
	}


	public Case(String recordUser) {
		super();
		this.recordUser = recordUser;
	}
	
	@Transient
	public List<PoliceCode> getActualPoliceCode(){
		return this.getLastReport().getPoliceCodeList();
	}
	
	
//	public List<AppEntity> getActualSuspects(){
//		List<EntityReportAssociation> eraList = this.getLastReport().getEntityReportAssocList();
//		List<AppEntity> list = new ArrayList<AppEntity>();
//		
//		if(eraList != null){
//			for (EntityReportAssociation era : eraList) {
//				if(era.getEntityReportAssocValue().getTag().equals("I DYSHUAR")){
//					
//					list.add(era.getAppEntity());
//				}
//			}
//		}
//		
//		if(list.size() > 0) 
//			return list; 
//		else 
//			return null;
//	}
	
	public void addReport(Report report){
		if(this.reportList == null) this.reportList = new ArrayList<Report>();
		
		this.reportList.add(report);
	}
	
	

	public boolean hasEventReport(){
		if(this.reportList == null) return false;
		for (Report report : this.reportList) {
			if(report.getReportType().equals(ReportTypeEnum.ER)) 
				return true;
		}
		return false;
	}
	
	public boolean hasSupplementReport(){
		if(this.reportList == null) return false;
		for (Report report : this.reportList) {
			if(report.getReportType().equals(ReportTypeEnum.SR)) 
				return true;
		}
		return false;
	}
	
	public Report getEventReport(){
		if(this.getReportList() != null && !this.getReportList().isEmpty()){
			for (Report report : this.getReportList()) {
				if(report.getReportType().equals(ReportTypeEnum.ER))
					return report;
			}
		}
		
		return null;
	}
	
	public List<Report> getSupplementReportList(){
		if(this.getReportList() != null && !this.getReportList().isEmpty()){
			List<Report> tempList = new ArrayList<Report>();
			for (Report report : this.getReportList()) {
				if(report.getReportType().equals(ReportTypeEnum.SR))
					tempList.add(report);
			}
			if(!tempList.isEmpty()) return tempList;
			
		}
		
		return null;
	}
	
	public int getSupplementReportNumber(){
		int nr = 0;
		if(this.getReportList() != null && !this.getReportList().isEmpty()){
			
			for (Report report : this.getReportList()) {
				if(report.getReportType().equals(ReportTypeEnum.SR))
					nr +=1;
			}
		}
		return nr;
	}
	
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}



	public String getCaseId() {
		return caseId;
	}

	public void setCaseId(String caseId) {
		this.caseId = caseId;
	}



	public Date getRecordDate() {
		return recordDate;
	}

	public void setRecordDate(Date recordDate) {
		if(this.recordDate == null)
			this.recordDate = recordDate;
	}
	public String getRecordUser() {
		return recordUser;
	}
	public void setRecordUser(String recordUser) {
		this.recordUser = recordUser;
	}


	public List<Report> getReportList() {
		return reportList;
	}


	public void setReportList(List<Report> reportList) {
		this.reportList = reportList;
	}


	public Report getLastReport() {
		return lastReport;
	}


	public void setLastReport(Report lastReport) {
		this.lastReport = lastReport;
	}
}
